﻿using System;
using System.Collections.Generic;
using System.Text;
using GGS.TreeManage.Interfaces;

namespace GGS.TreeManage.TestConsole
{
    public class TestCLass
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TestINode();

            TestINodeT();

            Console.Read();
        }

        private static void TestINode()
        {
            INode rootNode = Factory.CreateINode("00");
            for (int i = 0; i < 20; i++)
            {
                Random r = new Random(i);
                INode child = Factory.CreateINode(i.ToString());
                child.Name = " Name " + i;
                child.OrderNum = r.Next(20);
                if (i <= 10)
                {
                    rootNode.AddChildNode(child);
                }
                else if (i >= 11 && i <= 15)
                {
                    rootNode.ChildNodeList[0].AddChildNode(child);
                    //rootNode.ChildNodeList[0].ChildNodeList.Add(child);
                }
                else
                {
                    rootNode.ChildNodeList[0].ChildNodeList[0].ChildNodeList.Add(child);
                }
            }
            Display("", rootNode);
            rootNode.SortByComparer((m, n) => m.NodeID.CompareTo(n.NodeID));
            Display("", rootNode);
            rootNode.RefreshOrder();
            Display("", rootNode);

            rootNode.SelectChecked(false);
            Display("Before ", rootNode);
            INode childNode11 = rootNode.FindNode("11");
            childNode11.SelectChecked(true);
            Display("Node11 true ", rootNode);
            childNode11.SelectChecked(false);
            Display("Node11 false ", rootNode);

            childNode11.SelectChecked(true);
            INode childNode13 = rootNode.FindNode("13");
            childNode13.SelectChecked(true);
            Display("Node11 13 true ", rootNode);
            childNode11.SelectChecked(false);
            Display("Node11 false 13 true ", rootNode);
        }

        private static void TestINodeT()
        {
            INode<TestCLass> rootNode = Factory.CreateINode<TestCLass>("00");
            rootNode.ExtraData = new TestCLass();

            for (int i = 0; i < 20; i++)
            {
                Random r = new Random(i);
                INode<TestCLass> child = Factory.CreateINode<TestCLass>(i.ToString());
                child.ExtraData = new TestCLass();
                child.ExtraData.Name = "Name" + i.ToString();
                child.OrderNum = r.Next(20);
                if (i <= 10)
                {
                    //rootNode.ChildNodeList.Add(child);
                    rootNode.AddChildNode(child);
                }
                else if (i >= 11 && i <= 15)
                {
                    //rootNode.ChildNodeList[0].ChildNodeList.Add(child);
                    rootNode.ChildNodeList[0].AddChildNode(child);
                }
                else
                {
                    //rootNode.ChildNodeList[0].ChildNodeList[0].ChildNodeList.Add(child);
                    rootNode.ChildNodeList[0].ChildNodeList[0].AddChildNode(child);
                }
            }
            Display("", rootNode);
            rootNode.SortByComparer((m, n) => m.ExtraData.Name.CompareTo(n.ExtraData.Name));
            Display("", rootNode);
            rootNode.RefreshOrder();
            Display("", rootNode);

            rootNode.SelectChecked(false);
            Display("Before ", rootNode);
            INode<TestCLass> childNode11 = rootNode.FindNode("11");
            childNode11.SelectChecked(true);
            Display("Node11 true ", rootNode);
            childNode11.SelectChecked(false);
            Display("Node11 false ", rootNode);

            childNode11.SelectChecked(true);
            INode<TestCLass> childNode13 = rootNode.FindNode("13");
            childNode13.SelectChecked(true);
            Display("Node11 13 true ", rootNode);
            childNode11.SelectChecked(false);
            Display("Node11 false 13 true ", rootNode);

            rootNode.RemoveChild("11");
            Display("RemoveChild Node11 ", rootNode);
        }
        private static void Display(string blank, INode<TestCLass> currentNode)
        {
            Console.WriteLine(blank + currentNode.NodeID + currentNode.Name + " T ordernum " + currentNode.OrderNum + " Checked " + currentNode.Checked);
            foreach (var item in currentNode.GetChildNoteList())
            {
                Display(blank + "  ", item);
            }
        }

        private static void Display(string blank, INode currentNode)
        {
            Console.WriteLine(blank + currentNode.NodeID+currentNode.Name+" ordernum "+currentNode.OrderNum+" Checked "+currentNode.Checked);
            foreach (var item in currentNode.ChildNodeList)
            {
                Display(blank + "  ", item);
            }
        }
    }
}
